package com.clouddong.exterapi.controller;

import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.clouddong.exterapi.service.IPConverServiceI;
import com.clouddong.exterapi.service.UserServiceI;
import com.clouddong.exterapi.util.IPConvertUtil;
import com.wordnik.swagger.annotations.ApiOperation;



/**
 * @author wyx
 * @version 1.0
 * @email wangyingxuan@dimpt.com
 * @Date 2018-4-10
 * @Time 下午4:40:37
 * @Description 
 */
@Controller
@RequestMapping("/mvc")
public class IPConverController {
 
    @Autowired
    private UserServiceI userServiceI;
    
    @Autowired
    private IPConverServiceI ipConverServiceI;
    
    Integer countInteger=0;
    
	@RequestMapping(value="/user/{id}",method=RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value = "根据用户名获取用户对象", httpMethod = "GET", response = String.class, notes = "根据用户名获取用户对象")
    public void get(@PathVariable Integer id,PrintWriter pw){
	    countInteger=countInteger+1;
	    System.out.println("get"+id+"编号："+countInteger);
        //return "/hello";
        pw.print("hello:"+id+"编号："+countInteger);
    }
     
    @RequestMapping(value="/user/{id}",method=RequestMethod.POST)
    public String post(@PathVariable("id") Integer id,HttpServletRequest request){
        String jsonstr1=request.getParameter("command");
        String jsonstr2=request.getParameter("timed");
        String jsonstr3=request.getParameter("test");
        String jsonstr4=request.getParameter("cleanStartMD5");
        String jsonstr5=request.getParameter("csvIP");
        System.out.println("post"+id+"command:"+jsonstr1+"time:"+jsonstr2+"IP:"+jsonstr5);
        return "/hello";
    }
    @ResponseBody
    @RequestMapping(value="/trantip",method=RequestMethod.POST)
    public String TrantIp(HttpServletRequest request){
        String ipss=request.getParameter("ips");
        String ips=ipss.replace(" ", "");
        String cbiid=request.getParameter("cbiid");
        if(ips.equals("")){return null;}
        String domaincode=request.getParameter("domaincode");
        if(domaincode.equals("0000")){domaincode=null;};
        ResourceBundle rb = ResourceBundle.getBundle("params");
        String param1 = rb.getString("param1");
        String param2 = rb.getString("param2");
        String param3 = rb.getString("param3");
        String param4 = rb.getString("param4");
        String param5 = rb.getString("param5");
//        ResourceBundle rb = ResourceBundle.getBundle("params");
//        String testjson = rb.getString("testjson");
       String [] iparrys=ips.split("\n");
       
       JSONArray jsonArray=new JSONArray();
       for(int i = 0; i < iparrys.length; i++){
           Map<String, String> ipmaps=new HashMap<String, String>();
           String ipmask=iparrys[i];
           String [] ipmaskarry=ipmask.split("/");
           String ip = ipmaskarry[0];
           int mask = Integer.parseInt(ipmaskarry[1]);
           //TestDemo testDemo=new TestDemo();
           long start = IPConvertUtil.ipToLong(ip);          // 地址
           long number = IPConvertUtil.ipCount(mask);        // ip地址个数
           long lmask = IPConvertUtil.maskToLong(mask);      // 掩码
           
           String startIp = (IPConvertUtil.longToIP(start & lmask)); //开始ip
           String endIp = (IPConvertUtil.longToIP((start & lmask) + number)); //结束ip
           
           long startIpLong=IPConvertUtil.ipToLong(startIp); //开始ip(10进制)
           long endIpLong=IPConvertUtil.ipToLong(endIp);  //结束ip(10进制)
           
           Date now=new Date();
           SimpleDateFormat sDateFormat=new SimpleDateFormat("yyyy-MM-dd");
           String nowDate=sDateFormat.format(now);
           String wtxts="'"+startIp+"'"+","+"'"+endIp+"'"+","+"'"+startIp+"'"+","+"'"+mask+"'"+","+"'"+startIpLong+"'"+","+"'"+endIpLong+"'";
           String wtxtsT=param1+cbiid+param2+wtxts+param3+domaincode+param4+nowDate+param5;

           System.out.println(wtxtsT);
           ipmaps.put("sql", wtxtsT);
           jsonArray.add(ipmaps);
           
    }
        JSONObject jsonObject=new JSONObject();
        // jsonObject=(JSONObject)JSONObject.toJSON(ipmaps);
        return jsonArray.toString();
    } 
    @ResponseBody
    @RequestMapping(value="/test",method=RequestMethod.GET)
    public Object test(){
        int ret=userServiceI.getstunum();
        System.out.println(ret);
        return "hello!!";
    }
    @RequestMapping(value="/additems",method=RequestMethod.GET,produces = {"text/html;charset=UTF-8;"})
    @ResponseBody
    @ApiOperation(value = "获取地域数据", httpMethod = "GET", response = String.class, notes = "获取地域数据")
    public String addItems(){
        List<Map<String, String>> domainItems=ipConverServiceI.addItems();
        net.sf.json.JSONArray domainItemsjson=net.sf.json.JSONArray.fromObject(domainItems);
        String domainItemsstr="{result:"+domainItemsjson.toString()+"}";
        return JSONObject.parse(domainItemsstr).toString();
    }
    @RequestMapping(value="/additemsJsonp",method=RequestMethod.GET,produces = {"text/html;charset=UTF-8;"})
    @ResponseBody
    @ApiOperation(value = "获取地域数据", httpMethod = "GET", response = String.class, notes = "获取地域数据")
    public String addItems(HttpServletRequest request){
        List<Map<String, String>> domainItems=ipConverServiceI.addItems();
        net.sf.json.JSONArray domainItemsjson=net.sf.json.JSONArray.fromObject(domainItems);
        String domainItemsstr="{result:"+domainItemsjson.toString()+"}";
        //前端传过来的回调函数名称
        String callback = request.getParameter("callback");
        //用回调函数名称包裹返回数据，这样，返回数据就作为回调函数的参数传回去了
        String result = callback + "(" + domainItemsstr + ")";
        return result;
    }
 
      
}
